package com.water.service.Impl;

import com.water.context.BaseContext;
import com.water.entity.Price;
import com.water.entity.User;
import com.water.entity.WaterUsage;
import com.water.mapper.PriceMapper;
import com.water.mapper.UserMapper;
import com.water.mapper.WaterUsageMapper;
import com.water.service.PriceService;
import com.water.vo.PriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: city_water
 * @description:
 * @author: lydms
 * @create: 2024-05-18 16:49
 **/

@Service
public class PriceServiceImpl implements PriceService {

    @Autowired
    private WaterUsageMapper waterUsageMapper;

    @Autowired
    private PriceMapper priceMapper;

    @Autowired
    private UserMapper userMapper;



    /**
     * 查询价格
     *
     * @return
     */
    @Override
    public PriceVo getInfo() {

        Long userId = BaseContext.getCurrentId();
        if (userId == null){
            throw new RuntimeException("用户未登录");
        }
        User user = userMapper.selectByUserId(userId);

        String region = user.getAddress();

        Price price = priceMapper.selectByRegion(region);
        //获得该地水价格
        Float p = price.getPrice();

        WaterUsage waterUsage = waterUsageMapper.getInfo(userId);

        //获得用户用水量
        Float consumption = waterUsage.getConsumption();

        //获得用户用水价格
        Float SPrice = consumption * p;
        PriceVo priceVo = PriceVo.builder()
                .price(price.getPrice())
                .region(price.getRegion())
                .SPrice(SPrice)
                .build();
        return priceVo;
    }
}
